using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
namespace DAL
{
    public class PrivateMessages : DAL.DataManager<PrivateMessage>
    {
        private static PrivateMessages Instance = new PrivateMessages();
        static PrivateMessages() { }

        public override string TableName
        {
            get { return @"yboard_pms"; }
        }

        protected override void CopyToItem(DataRow Row, PrivateMessage Item)
        {
            Item.To = Users.Item(Convert.ToInt64(Row["ToID"]));

            Item.From = Users.Item(Convert.ToInt64(Row["FromID"]));

            Item.Subject = Convert.ToString(Row["Subject"]);

            Item.Message = Convert.ToString(Row["Message"]);

            Item.DateSent = Convert.ToDateTime(Row["DateSent"]);

            Item.MsgRead = Convert.ToBoolean(Row["MsgRead"]);

            Item.MsgReplied = Convert.ToBoolean(Row["MsgReplied"]);

            Item.ToDeleted = Convert.ToBoolean(Row["ToDeleted"]);

            Item.FromDeleted = Convert.ToBoolean(Row["FromDeleted"]);
        }

        protected override void CopyFromItem(DataManagerQuery query, PrivateMessage Item)
        {
            query.AddParam("ToID", Item.To.ID);

            query.AddParam("FromID", Item.From.ID);

            query.AddParam("Subject", Item.Subject);

            query.AddParam("Message", Item.Message);

            query.AddParam("DateSent", Item.DateSent);

            query.AddParam("MsgRead", Item.MsgRead);

            query.AddParam("MsgReplied", Item.MsgReplied);

            query.AddParam("ToDeleted", Item.ToDeleted);

            query.AddParam("FromDeleted", Item.FromDeleted);
        }

        public static PrivateMessage Item(Int64 id)
        {
            return Instance.GetItem(id);
        }

        public static List<PrivateMessage> All()
        {
            return Instance.GetItems();
        }

        public static void Add(PrivateMessage Item)
        {
            Item.DateSent = DateTime.Now;
            if (Item.To.Status != User.UserStatus.Banned && Item.From.Status == User.UserStatus.Banned)
            {
                Item.ToDeleted = true;
            }
            Instance.Insert(Item);
        }

        public static void Save(PrivateMessage Item)
        {
            Instance.Update(Item);
        }

        public static PrivateMessage GetByIDAndUser(long id, User user)
        {
            PrivateMessage myMsg = Item(id);
            if (myMsg != null && (myMsg.To == user || myMsg.From == user))
            {
                return myMsg;
            }
            else
            {
                return null;
            }
        }

        public static List<PrivateMessage> GetInboxByUser(User user)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("ToID", user.ID);
            query.AddParam("ToDeleted", false);
            return Instance.GetItems(query);
        }

        public static DataManagerPage<PrivateMessage> GetInboxByUserAndPage(User user, int page)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("ToID", user.ID);
            query.AddParam("ToDeleted", false);
            query.AddSort("ID", Sort.Desc);
            return Instance.GetItemsByPage(query, page, 25);
        }

        public static List<PrivateMessage> GetOutboxByUser(User user)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("FromID", user.ID);
            query.AddParam("FromDeleted", false);
            query.AddParam("MsgRead", false);
            return Instance.GetItems(query);
        }

        public static DataManagerPage<PrivateMessage> GetOutboxByUserAndPage(User user, int page)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("FromID", user.ID);
            query.AddParam("MsgRead", false);
            query.AddParam("DateSent", Comparison.GreaterThanOrEqual, DateTime.Today.AddDays(-31));
            query.AddParam("FromDeleted", false);
            query.AddSort("ID", Sort.Desc);
            return Instance.GetItemsByPage(query, page, 25);
        }

        public static List<PrivateMessage> GetSentboxByUser(User user)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("FromID", user.ID);
            query.AddParam("FromDeleted", false);
            query.AddParam("MsgRead", true);
            return Instance.GetItems(query);
        }

        public static DataManagerPage<PrivateMessage> GetSentboxByUserAndPage(User user, int page)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("FromID", user.ID);
            query.AddParam("MsgRead", true);
            query.AddParam("FromDeleted", false);
            query.AddSort("ID", Sort.Desc);
            return Instance.GetItemsByPage(query, page, 25);
        }

        public static bool UserHasNewMessages(User user)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("ToID", user.ID);
            query.AddParam("ToDeleted", false);
            query.AddParam("MsgRead", false);
            return Instance.GetCount(query) > 0;
        }

        public static PrivateMessage GetNewestUnreadMessageByUser(User user)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("ToID", user.ID);
            query.AddParam("ToDeleted", false);
            query.AddParam("MsgRead", false);

            List<PrivateMessage> myMessages = Instance.GetItemsByPage(query, 1, 1).Items;
            if (myMessages.Count > 0)
            {
                return myMessages[0];
            }
            else
            {
                return null;
            }
        }
    }

    public class PrivateMessage : DAL.DataManagerRow
    {
        private User _To;
        public User To
        {
            get
            {
                return _To;
            }
            set
            {
                _To = value;
            }
        }


        private User _From;
        public User From
        {
            get
            {
                return _From;
            }
            set
            {
                _From = value;
            }
        }


        private string _Subject;
        public string Subject
        {
            get
            {
                return _Subject;
            }
            set
            {
                _Subject = value;
            }
        }


        private string _Message;
        public string Message
        {
            get
            {
                return _Message;
            }
            set
            {
                _Message = value;
            }
        }


        private DateTime _DateSent;
        public DateTime DateSent
        {
            get
            {
                return _DateSent;
            }
            set
            {
                _DateSent = value;
            }
        }


        private bool _MsgRead;
        public bool MsgRead
        {
            get
            {
                return _MsgRead;
            }
            set
            {
                _MsgRead = value;
            }
        }


        private bool _MsgReplied;
        public bool MsgReplied
        {
            get
            {
                return _MsgReplied;
            }
            set
            {
                _MsgReplied = value;
            }
        }


        private bool _ToDeleted;
        public bool ToDeleted
        {
            get
            {
                return _ToDeleted;
            }
            set
            {
                _ToDeleted = value;
            }
        }


        private bool _FromDeleted;
        public bool FromDeleted
        {
            get
            {
                return _FromDeleted;
            }
            set
            {
                _FromDeleted = value;
            }
        }
    }
}
